<template>
  <div>
    <!-- inline：代表的是行内表单，代表一行可以放置多个表单元素 -->
    <el-form :inline="true" class="demo-form-inline" :model="cFrom">
      <el-form-item label="一级分类">
        <el-select
          v-model="cFrom.category1Id"
          placeholder="请选择"
          @change="handler1"
          :disabled="show"
        >
          <el-option
            v-for="c1 in List1"
            :key="c1.id"
            :label="c1.name"
            :value="c1.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="二级分类">
        <el-select
          v-model="cFrom.category2Id"
          placeholder="请选择"
          @change="handler2"
          :disabled="show"
        >
          <el-option
            v-for="c2 in List2"
            :key="c2.id"
            :label="c2.name"
            :value="c2.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="三级分类">
        <el-select
          v-model="cFrom.category3Id"
          placeholder="请选择"
          @change="handler3"
          :disabled="show"
        >
          <el-option
            v-for="c3 in List3"
            :key="c3.id"
            :label="c3.name"
            :value="c3.id"
          ></el-option>
        </el-select>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "CategorySelect",
  props:['show'],
  data() {
    return {
      List1: [],
      List2: [],
      List3: [],
      // 收集相应的一级二级三级分类的id
      cFrom: {
        category1Id: "",
        category2Id: "",
        category3Id: "",
      },
    };
  },
  mounted() {
    // 获取一级分类
    this.getCategoryList();
  },
  methods: {
    // 获取一级分类事件的回调
    async getCategoryList() {
      let result = await this.$API.attr.reqCategory1List();
      if (result.code == 200) {
        this.List1 = result.data;
      }
    },
    // 获取二级分类
    async handler1() {
      // 清除数据
      this.List2 = [];
      this.List3 = [];
      this.cFrom.category2Id = "";
      this.cFrom.category3Id = "";
      const { category1Id } = this.cFrom;
      this.$emit("getCategoryId", category1Id, 1);
      let result = await this.$API.attr.reqCategory2List(category1Id);
      if (result.code == 200) {
        this.List2 = result.data;
      }
    },
    // 获取三级分类
    async handler2() {
      // 清除数据
      this.List3 = [];
      this.cFrom.category3Id = "";
      const { category2Id } = this.cFrom;
      this.$emit("getCategoryId", category2Id, 2);
      let result = await this.$API.attr.reqCategory3List(category2Id);
      if (result.code == 200) {
        this.List3 = result.data;
      }
    },
    handler3() {
      const { category3Id } = this.cFrom;
      this.$emit("getCategoryId", category3Id, 3);
    },
  },
};
</script>

<style lang="scss" scoped></style>
